package com.stars.pao.springbook.controller;

import com.stars.pao.springbook.constants.Constants;
import com.stars.pao.springbook.model.*;
import com.stars.pao.springbook.service.BookService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.bcel.classfile.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;

//    @RequestMapping("/getList")
//    public List<BookInfo> getList() {
////        1.从数据库获取数据
////        数据采用mock的方式
//
//        List<BookInfo> bookInfos = bookService.getBookInfoList();
//
////        3.返回数据
//        return bookInfos;
//    }

    @RequestMapping("/addBook")

    public Result addBook(BookInfo bookInfo) {
        log.info("添加图书, bookInfo:{}", bookInfo);
        //参数校验
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() <= 0
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())) {
            return Result.fail("参数错误");
        }
        //添加图书
        try {
            bookService.insertBook(bookInfo);
        } catch (Exception e) {
            return Result.fail("内部发生错误, 请联系管理员");
        }
        return Result.success("");
    }

    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>> getListByPage(PageRequest pageRequest) {
        log.info("查询列表信息，pageRequest:{}", pageRequest);

//        验证用户是否登录
//        UserInfo userInfo = (UserInfo) request.getSession().getAttribute(Constants.USER_SESSION_KEY);
//        if(userInfo == null || userInfo.getId() < 1) {
//            return Result.unLogin();
//        }
        if(pageRequest.getCurrentPage() < 1) {
            return Result.fail("参数非法");
        }

        PageResult<BookInfo> pageResult = bookService.getListByPage(pageRequest);

        return Result.success(pageResult);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId) {
        if(bookId < 1) {
            return null;
        }
        log.info("根据id查询图书信息，bookId:{}", bookId);
        return bookService.queryBookById(bookId);
    }

//    更新图书和删除图书
    @RequestMapping("/updateBook")
    public boolean updateBook(BookInfo bookInfo) {
        log.info("更新图书信息，bookInfo:{}", bookInfo);
        if(bookInfo.getId() < 0) {
            return false;
        }
        try {
           Integer result  =  bookService.updateBook(bookInfo);
           if (result <= 0) {
               return false;
           }
            return true;
        }  catch (Exception e) {
            log.error("更新图书信息失败 e：", e);
            return false;
        }
    }

//  批量删除
    @RequestMapping("/batchDelete")
    public boolean batchDelete(@RequestParam List<Integer> ids) {
        log.info("批量删除图书，ids:{}", ids);

        try {
            Integer result = bookService.batchDelete(ids);
            if (result < 0) {
                return false;
            }
            return true;
        }catch (Exception e){
            log.error("批量删除图书失败 ids:{},e:",ids,e);
            return false;
        }
    }
}
